home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / utils / nroff~06.zoo / text.c < prev    next >
C/C++ Source or Header  |  1992-07-18  |  18KB  |  1,077 lines

  1. static char *rcsid_text_c="$Id: text.c,v 1.2 1992/07/16 10:38:32 rosenkra Exp $";
  2.  
  3. /*
  4.  * $Log: text.c,v $
  5.  * Revision 1.2  1992/07/16  10:38:32  rosenkra
  6.  * port to gcc, add tm,ie,el
  7.  *
  8.  */
  9.  
  10. #undef OLD_WAY
  11. /*
  12.  *    text.c - text output processing portion of nroff word processor
  13.  *
  14.  *    adapted for atariST/TOS by Bill Rosenkranz 11/89
  15.  *    net:    rosenkra@convex.com
  16.  *    CIS:    71460,17
  17.  *    GENIE:    W.ROSENKRANZ
  18.  *
  19.  *    original author:
  20.  *
  21.  *    Stephen L. Browning
  22.  *    5723 North Parker Avenue
  23.  *    Indianapolis, Indiana 46220
  24.  *
  25.  *    history:
  26.  *
  27.  *    - Originally written in BDS C;
  28.  *    - Adapted for standard C by W. N. Paul
  29.  *    - Heavily hacked up to conform to "real" nroff by Bill Rosenkranz
  30.  */
  31.  
  32. #undef NRO_MAIN                    /* extern globals */
  33.  
  34. #include <stdio.h>
  35. #include "nroff.h"
  36.  
  37.  
  38. /*------------------------------*/
  39. /*    text            */
  40. /*------------------------------*/
  41. void text (p)
  42. REGISTER char  *p;
  43. {
  44.  
  45. /*
  46.  *    main text processing
  47.  */
  48.  
  49.     REGISTER int    i;
  50.     char        wrdbuf[MAXLINE];
  51.  
  52.  
  53.     /*
  54.      *   skip over leading blanks if in fill mode. we indent later.
  55.      *   since leadbl does a robrk, do it if in .nf mode
  56.      */
  57.     if (dc.fill == YES)
  58.     {
  59.         if (*p == ' ' || *p == '\n' || *p == '\r')
  60.             leadbl (p);
  61.     }
  62.     else
  63.         robrk ();
  64.  
  65.  
  66.     /*
  67.      *   expand escape sequences
  68.      */
  69.     expesc (p, wrdbuf);
  70.  
  71.  
  72.     /*
  73.      *   test for how to output
  74.      */
  75.     if (dc.ulval > 0)
  76.     {
  77.         /*
  78.          *   underline (.ul)
  79.          *
  80.          *   Because of the way underlining is handled,
  81.          *   MAXLINE should be declared to be three times
  82.          *   larger than the longest expected input line
  83.          *   for underlining.  Since many of the character
  84.          *   buffers use this parameter, a lot of memory
  85.          *   can be allocated when it may not really be
  86.          *   needed.  A MAXLINE of 180 would allow about
  87.          *   60 characters in the output line to be
  88.          *   underlined (remember that only alphanumerics
  89.          *   get underlined - no spaces or punctuation).
  90.          */
  91.         underl (p, wrdbuf, MAXLINE);
  92.         --dc.ulval;
  93.     }
  94.     if (dc.cuval > 0)
  95.     {
  96.         /*
  97.          *   continuous underline (.cu)
  98.          */
  99.         underl (p, wrdbuf, MAXLINE);
  100.         --dc.cuval;
  101.     }
  102.     if (dc.boval > 0)
  103.     {
  104.         /*
  105.          *   bold (.bo)
  106.          */
  107.         bold (p, wrdbuf, MAXLINE);
  108.         --dc.boval;
  109.     }
  110.     if (dc.ceval > 0)
  111.     {
  112.         /*
  113.          *   centered (.ce)
  114.          */
  115.         center (p);
  116.         do_mc (p);
  117.         put (p);
  118.         --dc.ceval;
  119.     }
  120.     else if ((*p == '\r' || *p == '\n') && dc.fill == NO)
  121.     {
  122.         /*
  123.          *   all blank line
  124.          */
  125.         do_mc (p);
  126.         put (p);
  127.     }
  128.     else if (dc.fill == NO)
  129.     {
  130.         /*
  131.          *   unfilled (.nf)
  132.          */
  133.         do_mc (p);
  134.         put (p);
  135.     }
  136.     else
  137.     {
  138.         /*
  139.          *   anything else...
  140.          *
  141.          *   init escape char counter for this line...
  142.          */
  143. /*        co.outesc = 0;*/
  144.  
  145.  
  146.         /*
  147.          *   get a word and put it out. increment ptr to the next
  148.          *   word.
  149.          */
  150.         while ((i = getwrd (p, wrdbuf)) > 0)
  151.         {
  152. /*            co.outesc += countesc (wrdbuf);*/
  153.  
  154.             putwrd (wrdbuf);
  155.             p += i;
  156.         }
  157.     }
  158. }
  159.  
  160.  
  161.  
  162.  
  163. /*------------------------------*/
  164. /*    bold            */
  165. /*------------------------------*/
  166. void bold (p0, p1, size)
  167. REGISTER char  *p0;
  168. REGISTER char  *p1;
  169. int        size;
  170. {
  171.  
  172. /*
  173.  *    insert bold face text (by overstriking)
  174.  */
  175.  
  176.     REGISTER int    i;
  177.     REGISTER int    j;
  178.  
  179.     j = 0;
  180.     for (i = 0; (p0[i] != '\n') && (j < size - 1); ++i)
  181.     {
  182.         if (isalpha (p0[i]) || isdigit (p0[i]))
  183.         {
  184.             p1[j++] = p0[i];
  185.             p1[j++] = '\b';
  186.         }
  187.         p1[j++] = p0[i];
  188.     }
  189.     p1[j++] = '\n';
  190.     p1[j] = EOS;
  191.     while (*p1 != EOS)
  192.         *p0++ = *p1++;
  193.     *p0 = EOS;
  194. }
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. /*------------------------------*/
  202. /*    center            */
  203. /*------------------------------*/
  204. void center (p)
  205. REGISTER char  *p;
  206. {
  207.  
  208. /*
  209.  *    center a line by setting tival
  210.  */
  211.  
  212.     dc.tival = max ((dc.rmval + dc.tival - width (p)) >> 1, 0);
  213. }
  214.  
  215.  
  216.  
  217.  
  218. /*------------------------------*/
  219. /*    expand            */
  220. /*------------------------------*/
  221. void expand (p0, c, s)
  222. REGISTER char  *p0;
  223. char        c;
  224. REGISTER char  *s;
  225. {
  226.  
  227. /*
  228.  *    expand title buffer to include character string
  229.  */
  230.  
  231.     REGISTER char  *p;
  232.     REGISTER char  *q;
  233.     REGISTER char  *r;
  234.     char        tmp[MAXLINE];
  235.  
  236.     p = p0;
  237.     q = tmp;
  238.     while (*p != EOS)
  239.     {
  240.         if (*p == c)
  241.         {
  242.             r = s;
  243.             while (*r != EOS)
  244.                 *q++ = *r++;
  245.         }
  246.         else
  247.             *q++ = *p;
  248.         ++p;
  249.     }
  250.     *q = EOS;
  251.     strcpy (p0, tmp);        /* copy it back */
  252. }
  253.  
  254.  
  255.  
  256.  
  257. /*------------------------------*/
  258. /*    justcntr        */
  259. /*------------------------------*/
  260. void justcntr (p, q, limit)
  261. REGISTER char  *p;
  262. char           *q;
  263. int           *limit;
  264. {
  265.  
  266. /*
  267.  *    center title text into print buffer
  268.  */
  269.  
  270.     REGISTER int    len;
  271.  
  272.     len = width (p);
  273.     q   = &q[(limit[RIGHT] + limit[LEFT] - len) >> 1];
  274.     while (*p != EOS)
  275.         *q++ = *p++;
  276. }
  277.  
  278.  
  279.  
  280.  
  281.  
  282. /*------------------------------*/
  283. /*    justleft        */
  284. /*------------------------------*/
  285. void justleft (p, q, limit)
  286. REGISTER char  *p;
  287. char           *q;
  288. int        limit;
  289. {
  290.  
  291. /*
  292.  *    left justify title text into print buffer
  293.  */
  294.  
  295.     q = &q[limit];
  296.     while (*p != EOS)
  297.         *q++ = *p++;
  298. }
  299.  
  300.  
  301.  
  302.  
  303. /*------------------------------*/
  304. /*    justrite        */
  305. /*------------------------------*/
  306. void justrite (p, q, limit)
  307. REGISTER char  *p;
  308. char           *q;
  309. int         limit;
  310. {
  311.  
  312. /*
  313.  *    right justify title text into print buffer
  314.  */
  315.  
  316.     REGISTER int    len;
  317.  
  318.     len = width (p);
  319.     q = &q[limit - len];
  320.     while (*p != EOS)
  321.         *q++ = *p++;
  322. }
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329. /*------------------------------*/
  330. /*    leadbl            */
  331. /*------------------------------*/
  332. void leadbl (p)
  333. REGISTER char  *p;
  334. {
  335.  
  336. /*
  337.  *    delete leading blanks, set tival
  338.  */
  339.  
  340.     REGISTER int    i;
  341.     REGISTER int    j;
  342.  
  343.     /*
  344.      *   end current line and reset co struct
  345.      */
  346.     robrk ();
  347.  
  348.     /*
  349.      *   skip spaces
  350.      */
  351.     for (i = 0; p[i] == ' ' || p[i] == '\t'; ++i)
  352.         ;
  353.  
  354.     /*
  355.      *   if not end of line, reset current temp indent
  356.      */
  357.     if (p[i] != '\n' && p[i] != '\r')
  358.         dc.tival = i;
  359.  
  360.     /*
  361.      *   shift string
  362.      */
  363.     for (j = 0; p[i] != EOS; ++j)
  364.         p[j] = p[i++];
  365.     p[j] = EOS;
  366. }
  367.  
  368.  
  369.  
  370.  
  371.  
  372. /*------------------------------*/
  373. /*    pfoot            */
  374. /*------------------------------*/
  375. void pfoot ()
  376. {
  377.  
  378. /*
  379.  *    put out page footer
  380.  */
  381.  
  382.     if (dc.prflg == TRUE)
  383.     {
  384.         skip (pg.m3val);
  385.         if (pg.m4val > 0)
  386.         {
  387.             if ((pg.curpag % 2) == 0)
  388.             {
  389.                 puttl (pg.efoot, pg.eflim, pg.curpag);
  390.             }
  391.             else
  392.             {
  393.                 puttl (pg.ofoot, pg.oflim, pg.curpag);
  394.             }
  395.             skip (pg.m4val - 1);
  396.         }
  397.     }
  398. }
  399.  
  400.  
  401.  
  402.  
  403.  
  404. /*------------------------------*/
  405. /*    phead            */
  406. /*------------------------------*/
  407. void phead ()
  408. {
  409.  
  410. /*
  411.  *    put out page header
  412.  */
  413.  
  414.     pg.curpag = pg.newpag;
  415.     if (pg.curpag >= pg.frstpg && pg.curpag <= pg.lastpg)
  416.     {
  417.         dc.prflg = TRUE;
  418.     }
  419.     else
  420.     {
  421.         dc.prflg = FALSE;
  422.     }
  423.     ++pg.newpag;
  424.     set_ireg ("%", pg.newpag, 0);
  425.     if (dc.prflg == TRUE)
  426.     {
  427.         if (pg.m1val > 0)
  428.         {
  429.             skip (pg.m1val - 1);
  430.             if ((pg.curpag % 2) == 0)
  431.             {
  432.                 puttl (pg.ehead, pg.ehlim, pg.curpag);
  433.             }
  434.             else
  435.             {
  436.                 puttl (pg.ohead, pg.ohlim, pg.curpag);
  437.             }
  438.         }
  439.         skip (pg.m2val);
  440.     }
  441.     /* 
  442.      *    initialize lineno for the next page
  443.      */
  444.     pg.lineno = pg.m1val + pg.m2val + 1;
  445.     set_ireg ("ln", pg.lineno, 0);
  446. }
  447.  
  448.  
  449.  
  450.  
  451. /*------------------------------*/
  452. /*    puttl            */
  453. /*------------------------------*/
  454. void puttl (p, lim, pgno)
  455. REGISTER char  *p;
  456. int           *lim;
  457. int        pgno;
  458. {
  459.  
  460. /*
  461.  *    put out title or footer
  462.  */
  463.  
  464.     REGISTER int    i;
  465.     char        pn[8];
  466.     char        t[MAXLINE];
  467.     char        h[MAXLINE];
  468.     char        delim;
  469.  
  470.     itoda (pgno, pn, 6);
  471.     for (i = 0; i < MAXLINE; ++i)
  472.         h[i] = ' ';
  473.     delim = *p++;
  474.     p = getfield (p, t, delim);
  475.     expand (t, dc.pgchr, pn);
  476.     justleft (t, h, lim[LEFT]);
  477.     p = getfield (p, t, delim);
  478.     expand (t, dc.pgchr, pn);
  479.     justcntr (t, h, lim);
  480.     p = getfield (p, t, delim);
  481.     expand (t, dc.pgchr, pn);
  482.     justrite (t, h, lim[RIGHT]);
  483.     for (i = MAXLINE - 4; h[i] == ' '; --i)
  484.         h[i] = EOS;
  485. #ifndef NO_CR
  486. /* note: the order of the next 2 stmts has been reversed... */
  487.     h[++i] = '\r';
  488. #endif
  489.     h[++i] = '\n';
  490.     h[++i] = EOS;
  491.     if (strlen (h) > 2)
  492.     {
  493.         for (i = 0; i < pg.offset; ++i)
  494.             prchar (' ', out_stream);
  495.     }
  496.     putlin (h, out_stream);
  497. }
  498.  
  499.  
  500.  
  501.  
  502.  
  503. /*------------------------------*/
  504. /*    putwrd            */
  505. /*------------------------------*/
  506. void putwrd (wrdbuf)
  507. REGISTER char  *wrdbuf;
  508. {
  509.  
  510. /*
  511.  *    put word in output buffer
  512.  */
  513.  
  514. #ifdef DO_HYPH
  515.     ch